/*
  Copyright 1996-2000
  Simon Whiteside

  $Id: SimkinXMLApplet.java,v 1.8 2003/01/22 18:26:34 simkin_cvs Exp $
*/
/**
 *
 * This applet shows how you can integrate Simkin into a Java application
 *
 * The applet reads an XML fragment from the TextArea control and executes the function "main" from within the fragment
 *
 **/
package simkin.examples.applet;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import org.xml.sax.*;
import java.io.*;
import java.util.StringTokenizer;
import simkin.*;


public class SimkinXMLApplet extends Applet implements TraceCallback,ActionListener{
static final String g_DefaultCode="<mydialog>\n<dialog width=\"300\" height=\"120\" title=\"Simkin Demo\">\n<controls>\n <control type=\"static\">\n  <text>This window and its behaviour are defined in Dialog.xml</text>\n </control>	\n <control type=\"panel\">\n  <controls>\n   <control type=\"static\" text=\"Prompt:\"/>\n   <control type=\"edit\" name=\"edit\"/>\n  </controls>\n </control>\n <control type=\"panel\">\n  <controls>\n   <control type=\"button\" method=\"okPressed\" text=\"OK\"/>\n   <control type=\"button\" method=\"cancelPressed\" text=\"Cancel\"/>\n  </controls>\n </control>\n</controls>\n</dialog>\n<function name=\"init\">\n setFocus(\"edit\"); \n</function>\n<function name=\"okPressed\">\n msgBox(\"You typed \" # getText(\"edit\")); \n close(); \n</function>\n<function name=\"cancelPressed\">\n msgBox(\"You cancelled the dialog!\");\n close(); \n</function>\n</mydialog>";
    /**
     * This textarea contains the XML which will be loaded
     */
    TextArea m_Script=new TextArea(20,80);
    /**
     * This is a listbox that shows trace messages
     */
    List m_Tracing=new List(10);
    /**
     * This button is pushed to execute the "main" function
     */
    Button m_RunMe=new Button("Run the script");
    /**
     * Interpreter to use for method calls
     */
    Interpreter m_Interpreter=new Interpreter();
    /**
     * Called when the applet starts
     */
    public void start(){
	// set ourselves as the tracing object so that we can put tracing messages into the list box
	m_Interpreter.setTraceCallback(this);

	// set up the user interface

	setLayout(new BorderLayout());
	Panel upperPanel=new Panel();
	upperPanel.setLayout(new BorderLayout());
	upperPanel.add("North",new Label("Simkin script embedded within XML:"));
	upperPanel.add("Center",m_Script);
	add("Center",upperPanel);
	Font font=new Font("Courier",Font.PLAIN,12);
	m_Script.setText(g_DefaultCode);
	m_Script.setFont(font);
	Panel lowerPanel=new Panel();
	lowerPanel.setLayout(new BorderLayout());
	lowerPanel.add("North",new Label("Trace output:"));
	lowerPanel.add("Center",m_Tracing);
	m_Tracing.setFont(font);
	lowerPanel.add("South",m_RunMe);
	add("South",lowerPanel);
	m_RunMe.addActionListener(this);
    }
    /**
     * This method is called when the Run button is pressed
     */
    public void actionPerformed(ActionEvent e){
	if (e.getSource()==m_RunMe)
	    executeCode(m_Script.getText());
    }
    /**
     * Load the XML from the text area control and call the "main" function
     */
    ScriptedDialog m_Dialog=null;
    public String executeCode(String code){
	String s="";
	try{
	    // Create an XMLExecutable object passing in the XML
	    if (m_Dialog==null)
		m_Dialog=new ScriptedDialog(new StringBufferInputStream(code),m_Interpreter);
	    else
		m_Dialog.reload(new StringBufferInputStream(code));
	}catch(RuntimeException r){
	    trace(r.toString());
	}catch(SAXException x){
	    trace(x.toString());
	}catch(IOException i){
	    trace(i.toString());
	}
	return s;
    }
    /**
     * Called when the applet is closed
     */
    public void stop(){
	removeAll();
    }
    /**
     * This is called to show an exception in the console
     */
    public void trace(Exception e){
	e.printStackTrace(System.out);
    }
    /**
     * This is called to show a trace message in the listbox
     */
    public void trace(String s){
	StringTokenizer tokenizer=new StringTokenizer(s,"\n");
	while (tokenizer.hasMoreElements()){
	    String line=tokenizer.nextToken();
	    m_Tracing.add(line);
	}
	int numLines=m_Tracing.getItemCount();
	if (numLines>0)
	    m_Tracing.makeVisible(numLines-1);
    }
}

